package com.yjb.demo.tdengine.controller;

import com.yjb.demo.tdengine.domain.dto.IotDevicePointDataDTO;
import com.yjb.demo.tdengine.domain.dto.IotDeviceStateDTO;
import com.yjb.demo.tdengine.domain.dto.TdInsertDTO;
import com.yjb.demo.tdengine.service.TaosService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * taos的通用例子
 * @author yinjinbiao
 * @create 2024/6/13 14:37
 */
@RestController
@RequestMapping("/taos")
public class TaosController {

    @Resource
    private TaosService taosService;

    /**
     * 新建子表，如果不存在超级表则也新增
     * @param stableName
     * @param deviceId
     * @param location
     */
    @GetMapping("/createTable")
    public void createTable(String stableName, Long deviceId, String location){
        taosService.createTable(stableName, deviceId, location);
    }

    /**
     * 添加列
     * @param stableName
     * @param columnName
     * @param columnType
     */
    @GetMapping("/addColumn")
    public void addColumn(String stableName, String columnName, String columnType){
        taosService.addColumn(stableName, columnName, columnType);
    }

    /**
     * 用于动态列的通用插入
     */
    @GetMapping("/insert")
    public void insert(){

        TdInsertDTO tdInsertDTO = new TdInsertDTO();
        tdInsertDTO.setDeviceId(1L);
        List columnList = new ArrayList<String>();
        columnList.add("current");
        tdInsertDTO.setColumnList(columnList);
        List valueList = new ArrayList<String>();
        valueList.add("1");
        tdInsertDTO.setValueList(valueList);
        taosService.insert(tdInsertDTO);
    }

    /**
     * 查询设备最新测点值
     * @param deviceId
     * @return
     */
    @GetMapping("/getLastRow")
    public IotDeviceStateDTO getLastRow(Long deviceId){
        IotDeviceStateDTO lastRow = taosService.getLastRow(deviceId);
        return lastRow;
    }

    /**
     * 查询设备最新测点值
     * @param deviceId
     * @return
     */
    @GetMapping("/getPointData")
    public List<IotDevicePointDataDTO> getPointData(Long deviceId, String columnName, Date startTime, Date endTime){
        return taosService.getPointData(deviceId, columnName, startTime, endTime);
    }
}
